package com.hsxxherp.ation.business.user.dto;

import com.hsxxherp.ation.common.error.AtionErrorException;
import com.hsxxherp.ation.common.error.AtionErrorMsgEnum;

import java.util.regex.Pattern;

/**
 * 用户邮箱登录前端请求实体
 *
 * @author Antonio 《2486970363@qq.com》
 * @since 2024年6月24日
 */
public class LoginByEmailRequest {

    private String email;
    private String password;

//    public LoginByEmailRequest(String email, String password) {
//        this.email = email;
//        this.password = password;
//    }

    // getter
    public String getEmail() {
        return email;
    }

    // setter
    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 参数验证器
     */
    public void validator() throws AtionErrorException {
        this.emailValidator();
    }

    /**
     * 验证邮箱
     *
     * @throws AtionErrorException
     */
    private void emailValidator() throws
            AtionErrorException {
        String EMAIL_PATTERN =
                "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

        Pattern pattern = Pattern.compile(EMAIL_PATTERN);

        if (email == null) {
            throw new AtionErrorException(AtionErrorMsgEnum.SYS_COMMON_PARAM_NO_CHECK,"登录邮箱不可为空！");
        }

        if (!pattern.matcher(email).matches()) {
            throw new AtionErrorException(AtionErrorMsgEnum.SYS_COMMON_PARAM_NO_CHECK,"邮箱格式不正确！");
        }
    }

}
